Pretty Good Privacy using C#.net


C#, System.Security.Cryptography.X509Certificates
Security
Desktop, Web
en-US
2/3/2016

Introduction

Pretty Good Privacy (PGP)  was created by Phil Zimmermann in 1991, Is a data encryption and decryption technique. Signing will play vital role in this encryption and Decryption.

 

Conceptual View

 

Step 1 : passphrase[Signature] ------- > Key Generator 

Step 2 : Key Generator ----------Gives ------- Public key and Private Key

Step 3: Original Text[Plain Text]  + Public Key + Private Key + Signature = PGP Encryption

Step 4: PGP Encrypted Text + Private Key + passphrase[Signature] = Original Text[Plain Text]

 

 

 

Building the Sample

To build the sample we need an API to get and generate symmetric and cipher keys.So i used BouncyCastle.Crypto.dll to acheive it.

Nuget Package Information : Install-Package BouncyCastle-Ext 

Description

To Implement PGP program we need to do work for generate keys, encryption and dercyption. From the conceptual view you must give sign to get keys. So most of the people using several third things like https://www.igolder.com/pgp/generate-key/ to get keys.

From this sample you can have a solution block to get keys also.

  

The below code snippet works to generate keys based on signature, password parametre is the signature value.

C#
Edit|Remove
public void KeyGeneration() 
        { 
            #region PublicKey and Private Key Generation 
 
            PGPSnippet.KeyGeneration.KeysForPGPEncryptionDecryption.GenerateKey("Username""password", @"[D:\Keys\]filelocation"); 
            Console.WriteLine("Keys Generated Successfully"); 
 
            #endregion 
        }

Step 2: Encryption part with public key, Private Key and signature

 

C#
Edit|Remove
public void Encryption() 
        { 
            #region PGP Encryption 
 
            PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(@"D:\Keys\PGPPublicKey.asc", @"D:\Keys\PGPPrivateKey.asc""P@ll@m@lli"); 
            PgpEncrypt encrypter = new PgpEncrypt(encryptionKeys); 
            using (Stream outputStream = File.Create("D:\\Keys\\EncryptData.txt")) 
            { 
                encrypter.EncryptAndSign(outputStream, new FileInfo(@"D:\Keys\PlainText.txt")); 
            } 
            Console.WriteLine("Encryption Done !"); 
 
            #endregion 
        }
 
Step 3: PGP Encrypted Text + Private Key + passphrase[Signature] = Original Text[Plain Text]
C#
Edit|Remove
public void Decryption() 
        { 
 
            #region PGP Decryption 
 
            PGPDecrypt.Decrypt("D:\\Keys\\EncryptData.txt", @"D:\Keys\PGPPrivateKey.asc""P@ll@m@lli""D:\\Keys\\OriginalText.txt"); 
 
            Console.WriteLine("Decryption Done"); 
 
            #endregion 
        }
 Step 4: Implementation 
C#
Edit|Remove
 static void Main(string[] args) 
        { 
 
            Program objPGP = new Program(); 
 
            try 
            { 
 
                objPGP.KeyGeneration(); 
                objPGP.Encryption(); 
                objPGP.Decryption(); 
 
            } 
            catch(Exception ex) 
            { 
                Console.WriteLine("Some thing went wrong"); 
                Console.Read(); 
            } 
        }
 

 

 

Source Code Files

More Information

For more information on this artical please verify

https://en.wikipedia.org/wiki/Pretty_Good_Privacy

https://www.bouncycastle.org/

https://www.nuget.org/packages/BouncyCastle-Ext/

http://www.c-sharpcorner.com/UploadFile/0d5b44/pgp-pretty-good-privacy-using-C-Sharp/